package com.crane.oss.web.controller.school;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.crane.common.core.dto.MultiResponse;
import com.crane.common.core.dto.Response;
import com.crane.common.core.dto.SingleResponse;
import com.crane.oss.school.bean.request.QueryAreaReq;
import com.crane.oss.school.bean.request.area.AddAreaReq;
import com.crane.oss.school.bean.request.area.UpdateAreaReq;
import com.crane.oss.school.bean.resp.AreaResp;
import com.crane.oss.school.service.AreaService;
import com.crane.oss.web.BaseBusinessController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * @author zhenxing.pi
 * @email ccsu123456@qq.com
 * @date 2022年03月26日 14时24分54秒
 */

@Api(value = "AreaController", tags = {"学校校区表"})
@RestController
@RequestMapping("/school/area")
public class AreaController extends BaseBusinessController {

    @Autowired
    AreaService areaService;


    @ApiOperation(value = "获取学校校区列表信息", notes = "获取列表信息")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryAreaReq")
    @RequiresPermissions("school:area:list")
    @PostMapping("/list")
    public MultiResponse<AreaResp> list(@RequestBody QueryAreaReq request) {
        request.setPartyId(getPartyId());
        IPage<AreaResp> page = areaService.queryList(request);
        return MultiResponse.of(page.getRecords(), (int) page.getTotal(), request);
    }



    @ApiOperation(value = "新增学校校区", notes = "获取新增信息")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "AddAreaReq")
    @RequiresPermissions("school:area:save")
    @PostMapping("/save")
    public Response save(@RequestBody @Valid AddAreaReq request) {
        request.setPartyId(getPartyId());
        request.setCreateUserId(getUserId());
        int count = areaService.save(request);
        return Response.buildSuccess();
    }

    @ApiOperation(value = "修改学校校区", notes = "修改")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "UpdateAreaReq")
    @RequiresPermissions("school:area:update")
    @PostMapping("/update")
    public Response update(@RequestBody @Valid UpdateAreaReq request) {
        request.setUpdateUserId(getUserId());
        request.setUpdatePartyId(getPartyId());
        int count = areaService.update(request);
        return Response.buildSuccess();
    }

    @ApiOperation(value = "删除学校校区", notes = "删除")
    @ApiImplicitParam(name = "schoolId", value = "请求对象", paramType = "path", dataType = "Long")
    @RequiresPermissions("school:area:delete")
    @GetMapping("/delete/{id}")
    public Response delete(@PathVariable("id") Long id) {
        int count = areaService.delete(id,getPartyId());
        return Response.buildSuccess();
    }


}
